package com.example.demo.mq.config;

import jakarta.annotation.PostConstruct;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MqConfig {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Bean
    public Queue ScoreQueue() {
        return new Queue(MqConstant.SCORE_QUEUE, true);
    }

    //Direct交换机 起名：TestDirectExchange
    @Bean
    DirectExchange BJJiuDirectExchange() {
        return new DirectExchange(MqConstant.SCORE_EXCHANGE);
    }

//    @Bean
//    DirectExchange TestDirectExchange() {
//        return new FanoutExchange(MqConstant.SCORE_EXCHANGE);
//    }

    @Bean
    Binding bindingBeiJingDirect() {
        return BindingBuilder.bind(ScoreQueue()).to(BJJiuDirectExchange()).with(MqConstant.SCORE_ROUTE_KEY);
    }


    @PostConstruct
    public void init() {

        // 消息返回, yml需要配置 publisher-returns: true
        rabbitTemplate.setReturnsCallback(new MessageReturnCallback());

        // 消息确认, yml需要配置 publisher-confirms: true
        rabbitTemplate.setConfirmCallback(new MessageConfirmCallback());

    }

}
